/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nutmotion.service;

import com.nutmotion.utils.StringBuilderAppendHelper;
import com.nutmotion.common.Constants;

/**
 *
 * @author Manu
 */
public class WhereClauseStringBuilder {

    private StringBuilderAppendHelper sbah;

    public WhereClauseStringBuilder() {
        this.sbah = new StringBuilderAppendHelper(Constants.JPQL_WHERE_CLAUSE_PREFIX, Constants.JPQL_WHERE_CLAUSE_APPEND);
    }
    
    private Object escapeStringValues(Object object) {
        if (object instanceof String) {
            return "'"+object+"'";
        }
        else 
            return object;
    }
    
    /**
     * If valueForfield is not null, appends a JPQL WHERE clause part for this pathToEntityField field
     * @param pathToEntityField JPQL path to the field
     * @param valueForField     the value for the field
     * @return 
     */
    public WhereClauseStringBuilder append(String pathToEntityField, Object valueForField) {
        if (valueForField != null) {
            this.sbah.append(pathToEntityField + Constants.JPQL_WHERE_CLAUSE_EQUALITY + this.escapeStringValues(valueForField));
        }

        return this;
    }

    @Override
    public String toString() {
        return this.sbah.toString();
    }
}
